package com.mobile.challenge.ui.report.unit.admin.weekmonth

import android.app.Application
import android.graphics.Color
import androidx.lifecycle.MutableLiveData
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.mobile.challenge.ui.report.unit.admin.AdminUtils
import com.mobile.challenge.ui.report.utils.IntegerFormat
import com.mobile.frame.mvvmframe.base.BaseModel
import com.mobile.frame.mvvmframe.base.DataViewModel
import javax.inject.Inject

class AdminHistogramViewModel @Inject constructor(application: Application, model: BaseModel?) : DataViewModel(application, model) {
    var barData = MutableLiveData<BarData>()


    fun generateData(unitName: String) {
        val dataBean = AdminUtils.getInstance().getAdminWeekMonthDataBean(unitName)
        val size = dataBean.trackingList.size
        // 数据
        val barNA = ArrayList<BarEntry>(0)
        val barOffice = ArrayList<BarEntry>(0)
        val barVacation = ArrayList<BarEntry>(0)
        val barWFH = ArrayList<BarEntry>(0)
        for (i in 0 until size) {
            val weekMonthDataBean = dataBean.trackingList[i]
            barNA.add(BarEntry(i.toFloat(), weekMonthDataBean.group.nacount.toFloat()))
            barOffice.add(BarEntry(i.toFloat(), weekMonthDataBean.group.officeCount.toFloat()))
            barVacation.add(BarEntry(i.toFloat(), weekMonthDataBean.group.leaveCount.toFloat()))
            barWFH.add(BarEntry(i.toFloat(), weekMonthDataBean.group.workHomeCount.toFloat()))
        }

        barNA.add(BarEntry(size.toFloat(), 0f))
        barOffice.add(BarEntry(size.toFloat(), 0f))
        barVacation.add(BarEntry(size.toFloat(), 0f))
        barWFH.add(BarEntry(size.toFloat(), 0f))


        // y 轴数据集
        val barDataSet = BarDataSet(barNA, "NA")
        barDataSet.color = Color.parseColor("#f38181")

        // y2 轴数据集
        val barDataSet2 = BarDataSet(barOffice, "Office")
        barDataSet2.color = Color.parseColor("#fce38a")

        // y3 轴数据集
        val barDataSet3 = BarDataSet(barVacation, "Vacation")
        barDataSet3.color = Color.parseColor("#eaffd0")

        // y4 轴数据集
        val barDataSet4 = BarDataSet(barWFH, "WFH")
        barDataSet4.color = Color.parseColor("#95e1d3")

        val data = BarData(barDataSet, barDataSet2, barDataSet3, barDataSet4)

        val groupSpace = 0.2f
        val barSpace = 0.00f
        val barWidth = 0.2F
        // 设置 柱子宽度
        data.barWidth = barWidth
        data.groupBars(0.0f, groupSpace, barSpace)

        data.setValueFormatter(IntegerFormat())

        barData.value = data
    }

}